package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.Honor;
import com.cfets.annualAssessment.entity.Monitor;
import com.cfets.annualAssessment.mapper.HonorMapper;
import com.cfets.annualAssessment.mapper.MonitorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/1/17.
 */
@Service
public class MonitorService {
    @Autowired
    private MonitorMapper monitorMapper;

    @Autowired
    private HonorMapper honorMapper;

    public List<Monitor> getMonitorPage(Monitor monitor) {
        Page<?> page = PageHelper.startPage(monitor.getPage(), monitor.getRows());
        List<Monitor> monitorList=null;
        monitorList = this.getMonitors(monitor);
        monitor.setTotalCount(page.getTotal());
        return monitorList;
    }
    public List<Monitor> getMonitors(Monitor monitor) {
        List<Monitor> monitorList=null;
        Example example=new Example(Monitor.class);
        example.setOrderByClause("date DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(monitor.getId_num())){
            criteria.andEqualTo("id_num",monitor.getId_num());
        }
        if(!StringUtils.isEmpty(monitor.getName())){
            criteria.andLike("name","%"+monitor.getName()+"%");
        }
        if(!StringUtils.isEmpty(monitor.getCa_num())){
            criteria.andEqualTo("ca_num",monitor.getCa_num());
        }
        if(!StringUtils.isEmpty(monitor.getOrg_name())){
            criteria.andLike("org_name","%"+monitor.getOrg_name()+"%");
        }
        if(!StringUtils.isEmpty(monitor.getDate())){
            criteria.andEqualTo("date",monitor.getDate());
        }
        if(!StringUtils.isEmpty(monitor.getMonitor())){
            criteria.andEqualTo("monitor",monitor.getMonitor());
        }
        monitorList = monitorMapper.selectByExample(example);
        return monitorList;
    }
    public boolean newMonitor(Monitor monitor) {
        return monitorMapper.insertSelective(monitor)>0;
    }

    public List<Monitor> AllMonitor() {
        return monitorMapper.selectAll();
    }

    public boolean updateMonitor(Monitor monitor) {
        return monitorMapper.updateByPrimaryKey(monitor)>0;
    }

    public boolean deleteMonitorById(int id) {
        return monitorMapper.deleteByPrimaryKey(id)>0;
    }

    public List<Honor> getHonorPage(Honor honor) {
        Page<?> page = PageHelper.startPage(honor.getPage(), honor.getRows(), "id");
        List<Honor> honorList=null;
        honorList = this.getHonors(honor);
        honor.setTotalCount(page.getTotal());
        return honorList;
    }

    public List<Honor> getHonors(Honor honor) {
        List<Honor> honorList=null;
        Example example=new Example(Honor.class);
        example.setOrderByClause("date DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(honor.getId_num())){
            criteria.andEqualTo("id_num",honor.getId_num());
        }
        if(!StringUtils.isEmpty(honor.getName())){
            criteria.andLike("name","%"+honor.getName()+"%");
        }
        if(!StringUtils.isEmpty(honor.getCa_num())){
            criteria.andEqualTo("ca_num",honor.getCa_num());
        }
        if(!StringUtils.isEmpty(honor.getOrg_name())){
            criteria.andLike("org_name","%"+honor.getOrg_name()+"%");
        }
        if(!StringUtils.isEmpty(honor.getDate())){
            criteria.andEqualTo("date",honor.getDate());
        }
        if(!StringUtils.isEmpty(honor.getHonor())){
            criteria.andEqualTo("honor",honor.getHonor());
        }
        honorList = honorMapper.selectByExample(example);
        return honorList;
    }

    public boolean newHonor(Honor honor) {
        return honorMapper.insertSelective(honor)>0;
    }

    public List<Honor> AllHonor() {
        return honorMapper.selectAll();
    }

    public boolean updateHonor(Honor honor) {
        return honorMapper.updateByPrimaryKey(honor)>0;
    }

    public boolean deleteHonorById(int id) {
        return honorMapper.deleteByPrimaryKey(id)>0;
    }
}
